<?php

namespace App\Http\Controllers\Frontend\Project;

use App\Http\Controllers\Controller;
use App\Http\Resources\Frontend\ApiResource;
use App\Http\Resources\Frontend\ProjectResource;
use App\Services\Contract\ProjectServiceInterface;
use App\Table\Constant\Project;
use Illuminate\Http\Request;

class ProjectController extends Controller
{

    /**
     * @var ProjectServiceInterface
     */
    protected $projectService;

    public function __construct(ProjectServiceInterface $projectService)
    {
        $this->projectService = $projectService;
    }

    function index(){
        $projects = $this->projectService->paginate([
            'user_id' => auth()->id(),
            'template' => Project::TEMPLATE_NO,
            'status' => Project::STATUS_NORMAL
        ], 15);
        return ProjectResource::collection($projects);
    }

    /**
     * @param Request $request
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Exception
     * @return mixed
     */
    function create(Request $request) {
        $this->validate($request, [
            'tid' => 'required',
            'title' => 'required'
        ]);
        $tid = $request->post('tid');
        $title = $request->post('title');

        $project = $this->projectService->create($tid, $title);

        if(is_null($project)) {
            return new ApiResource($this->projectService->getError(), 0);
        }
        return new ProjectResource($project);
    }

    /**
     * @param Request $request
     * @return ApiResource
     * @throws \Illuminate\Validation\ValidationException
     */
    function rename(Request $request) {
        $this->validate($request, [
            'pid' => 'required',
            'title' => 'required'
        ]);
        $pid = $request->post('pid');
        $title = $request->post('title');
        if(!$this->projectService->rename($pid, $title)) {
            return new ApiResource($this->projectService->getError(), 0);
        }
        return new ApiResource();
    }

    /**
     * @param Request $request
     * @return ApiResource|ProjectResource
     * @throws \Illuminate\Validation\ValidationException
     */
    function copy(Request $request){
        $this->validate($request, [
            'pid' => 'required',
        ]);
        $pid = $request->post('pid');
        $project = $this->projectService->copy($pid);

        if(is_null($project)) {
            return new ApiResource($this->projectService->getError(), 0);
        }
        return new ProjectResource($project);
    }

    /**
     * @param Request $request
     * @return ApiResource
     * @throws \Illuminate\Validation\ValidationException
     */
    function trash(Request $request) {
        $this->validate($request, [
            'pid' => 'required',
        ]);
        $pid = $request->post('pid');
        if(!$this->projectService->trash($pid)) {
            return new ApiResource($this->projectService->getError(), 0);
        }
        return new ApiResource();
    }

    /**
     * @param Request $request
     * @return ApiResource
     * @throws \Illuminate\Validation\ValidationException
     */
    function restore(Request $request) {
        $this->validate($request, [
            'pid' => 'required',
        ]);
        $pid = $request->post('pid');
        if(!$this->projectService->restore($pid)) {
            return new ApiResource($this->projectService->getError(), 0);
        }
        return new ApiResource();
    }

    function trashList() {
        $projects = $this->projectService->paginate([
            'user_id' => auth()->id(),
            'template' => Project::TEMPLATE_NO,
            'status' => Project::STATUS_TRASH
        ], 15);
        return ProjectResource::collection($projects);
    }
}
